package org.dromara.legal.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.legal.domain.vo.ConsultVo;
import org.dromara.legal.domain.bo.ConsultBo;
import org.dromara.legal.service.IConsultService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 咨询
 *
 * @author zhuqingrun
 * @date 2024-10-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/legal/consult")
public class ConsultController extends BaseController {

    private final IConsultService consultService;

    /**
     * 查询咨询列表
     */
    @SaCheckPermission("legal:consult:list")
    @GetMapping("/list")
    public TableDataInfo<ConsultVo> list(ConsultBo bo, PageQuery pageQuery) {
        String userType = LoginHelper.getLoginUser().getUserType();
        if(userType.equals(UserType.APP_USER.getUserType())) {
            bo.setCreateBy(LoginHelper.getUserId());
        } else {
            bo.setLawyerId(LoginHelper.getUserId());
        }
        return consultService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询咨询列表
     */
    @SaCheckPermission("legal:consult:list")
    @GetMapping("/listAudit")
    public TableDataInfo<ConsultVo> listAudit(ConsultBo bo, PageQuery pageQuery) {
        return consultService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出咨询列表
     */
    @SaCheckPermission("legal:consult:export")
    @Log(title = "咨询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ConsultBo bo, HttpServletResponse response) {
        List<ConsultVo> list = consultService.queryList(bo);
        ExcelUtil.exportExcel(list, "咨询", ConsultVo.class, response);
    }

    /**
     * 获取咨询详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("legal:consult:query")
    @GetMapping("/{id}")
    public R<ConsultVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(consultService.queryById(id));
    }

    /**
     * 新增咨询
     */
    @SaCheckPermission("legal:consult:add")
    @Log(title = "咨询", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ConsultBo bo) {
        return toAjax(consultService.insertByBo(bo));
    }

    /**
     * 修改咨询
     */
    @SaCheckPermission("legal:consult:edit")
    @Log(title = "咨询", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ConsultBo bo) {
        return toAjax(consultService.updateByBo(bo));
    }

    /**
     * 删除咨询
     *
     * @param ids 主键串
     */
    @SaCheckPermission("legal:consult:remove")
    @Log(title = "咨询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(consultService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 查询咨询回复列表
     */
    @GetMapping("/listReply")
    public ConsultVo listReply(ConsultBo bo) {
        return consultService.queryReply(bo);
    }

    /**
     * 查询我的咨询列表
     */
    @SaCheckPermission("legal:consult:list")
    @GetMapping("/myConsult")
    public TableDataInfo<ConsultVo> myConsult(ConsultBo bo, PageQuery pageQuery) {
        bo.setCreateBy(LoginHelper.getUserId());
        return consultService.queryPageList(bo, pageQuery);
    }

}
